﻿using System;
using System.IO;
using Microsoft.AspNetCore.Http;

namespace com.uplus.common.Tools
{
    public static class SaveFileMgr
    {
        public enum SaveType
        {
            Materiels,
            CustInfos,
            TerImgs,
            Others
        };
        /// <summary>
        /// 保存文件并返回文件全路径及文件名称（wwwroot之后的路径）
        /// </summary>
        /// <param name="save">保存文件的类型</param>
        /// <returns></returns>
        public static string FullPath(IFormFile file, SaveType save, bool isMd5FileName)
        {
            DateTime dt = DateTime.UtcNow;
            string fileName = DataConvert.Md5(dt.ToString() + Path.GetFileNameWithoutExtension(file.FileName)) + Path.GetExtension(file.FileName);
            if (!isMd5FileName)
                fileName = $"{ DateTime.Now.ToCstTime().ToString("yyyyMMddHHmmssfff")}{ Path.GetExtension(file.FileName)}";
            string basePath = Directory.GetCurrentDirectory() + @"\wwwroot";
            string path = @"\File\" + save.ToString();
            string dirPath = basePath + path;
            if (!Directory.Exists(dirPath))
                Directory.CreateDirectory(dirPath);
            string fileFullPath = dirPath + @"\" + fileName;
            using (FileStream fs = File.Create(fileFullPath))
            {
                file.CopyTo(fs);
                fs.Flush();
            }
            return path + @"\" + fileName;
        }
        /// <summary>
        /// 保存文件返回文件名称（带后缀）
        /// </summary>
        /// <param name="save">保存文件的类型</param>
        /// <returns></returns>
        public static string FullName(IFormFile file, SaveType save, bool isMd5FileName)
        {
            DateTime dt = DateTime.UtcNow;
            string fileName = DataConvert.Md5(dt.ToString() + Path.GetFileNameWithoutExtension(file.FileName)) + Path.GetExtension(file.FileName);
            if (!isMd5FileName)
                fileName = $"{ DateTime.Now.ToCstTime().ToString("yyyyMMddHHmmssfff")}{ Path.GetExtension(file.FileName)}";
            string basePath = Directory.GetCurrentDirectory() + @"\wwwroot";
            string path = @"\File\" + save.ToString();
            string dirPath = basePath + path;
            if (!Directory.Exists(dirPath))
                Directory.CreateDirectory(dirPath);
            string fileFullPath = dirPath + @"\" + fileName;
            using (FileStream fs = File.Create(fileFullPath))
            {
                file.CopyTo(fs); 
                fs.Flush();
            }
            return path + @"\" + fileName;
        }
        /// <summary>
        /// 保存文件返回文件名称（带后缀）
        /// </summary>
        /// <param name="save">保存文件的类型</param>
        /// <returns></returns>
        public static string FileName(IFormFile file, SaveType save, bool isMd5FileName)
        {
            DateTime dt = DateTime.UtcNow;
            string fileName = DataConvert.Md5(dt.ToString() + Path.GetFileNameWithoutExtension(file.FileName)) + Path.GetExtension(file.FileName);
            if (!isMd5FileName)
                fileName = $"{ DateTime.Now.ToCstTime().ToString("yyyyMMddHHmmssfff")}{ Path.GetExtension(file.FileName)}";
            string basePath = Directory.GetCurrentDirectory() + @"\wwwroot";
            string path = @"\File\" + save.ToString();
            string dirPath = basePath + path;
            if (!Directory.Exists(dirPath))
                Directory.CreateDirectory(dirPath);
            string fileFullPath = dirPath + @"\" + fileName;
            using (FileStream fs = File.Create(fileFullPath))
            {
                file.CopyTo(fs);
                fs.Flush();
            }
            return fileName;
        }
        /// <summary>
        /// 保存文件无返回
        /// </summary>
        /// <param name="save">保存文件的类型</param>
        /// <returns></returns>
        public static void Save(IFormFile file, SaveType save, bool isMd5FileName)
        {
            DateTime dt = DateTime.UtcNow;
            string fileName = DataConvert.Md5(dt.ToString() + Path.GetFileNameWithoutExtension(file.FileName)) + Path.GetExtension(file.FileName);
            if (!isMd5FileName)
                fileName = $"{ DateTime.Now.ToCstTime().ToString("yyyyMMddHHmmssfff")}{ Path.GetExtension(file.FileName)}";
            string basePath = Directory.GetCurrentDirectory() + @"\wwwroot";
            string path = @"\File\" + save.ToString();
            string dirPath = basePath + path;
            if (!Directory.Exists(dirPath))
                Directory.CreateDirectory(dirPath);
            string fileFullPath = dirPath + @"\" + fileName;
            using (FileStream fs = File.Create(fileFullPath))
            {
                file.CopyTo(fs);
                fs.Flush();
            }
        }
    }
}
